// pages/seatChoose/seatChoose.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movieName:"",
    color: "white",
    colorArray: [],
    row: 2,
    column: 2,
    seatInfoArray: [],
    totalPrice: 0,
    scale: 1,
    seatArray: [
      [0,1,1,"white"],
      [0,1,2,"white"],
      [0,1,3,"white"]
    ],
    seatObjectArray:[
      {
        id: 0,
        row: 1,
        column: 1,
        color: "white"
      },
      {
        id: 0,
        row: 1,
        column: 2,
        color: "white"
      },
      {
        id: 0,
        row: 1,
        column: 3,
        color: "white"
      }
    ]
  },

  scaleUp(){
    this.setData({
      scale: 2
    })
  },

  scaleDown(){
    this.setData({
      scale: 1
    })
  },

  seatSelect(event){
    console.log(event.currentTarget)
    var seatObjectArrayTmp = this.data.seatObjectArray
    var seatInfoArrayTmp = this.data.seatInfoArray
    var index = event.currentTarget.dataset.id
    var priceTmp = this.data.totalPrice
    var seatInfo = event.currentTarget.dataset.row +"排"+
    event.currentTarget.dataset.column+"座"
    // console.log(seatInfo)
    var numbers= seatInfo.match(/\d+/g);
    // console.log((Number(numbers[0]-1))*15+Number((numbers[1])-1))
    var seatIndex 
    if(seatObjectArrayTmp[index].color == "white"){
      seatObjectArrayTmp[index].color  = "lightgreen",
      seatInfoArrayTmp.push(seatInfo)
      priceTmp+= 43
    }
    else if(seatObjectArrayTmp[index].color  == "lightgreen"){
      seatObjectArrayTmp[index].color  = "white",
      seatIndex = seatInfoArrayTmp.indexOf(seatInfo)
      if(seatIndex>-1){
        seatInfoArrayTmp.splice(seatIndex,1)
        priceTmp-=43
      }
    }
    else{
    }

    this.setData({
      seatObjectArray: seatObjectArrayTmp,
      seatInfoArray: seatInfoArrayTmp,
      totalPrice: priceTmp
    })
  },

  cancel(event){
    var seatInfoArrayTmp = this.data.seatInfoArray
    var priceTmp = this.data.totalPrice
    var seatObjectArrayTmp = this.data.seatObjectArray
    // console.log(seatInfoArrayTmp)
    // console.log(event.currentTarget.dataset.seatinfo)
    var seatInfo = event.currentTarget.dataset.seatinfo
    var seatIndex = seatInfoArrayTmp.indexOf(seatInfo)
    if(seatIndex >- 1){
      seatInfoArrayTmp.splice(seatIndex,1)
      priceTmp -= 43

      // 从座位信息中提取排和座号
      var numbers = seatInfo.match(/\d+/g)
      var row = Number(numbers[0])
      var column = Number(numbers[1])
      
      // 计算对应的座位数组索引
      var seatId = (row - 1) * 15 + (column - 1)

      // 更新座位颜色为白色
      if (seatObjectArrayTmp[seatId]) {
        seatObjectArrayTmp[seatId].color = "white"
      }
    }
    
    this.setData({
      seatObjectArray: seatObjectArrayTmp,
      seatInfoArray: seatInfoArrayTmp,
      totalPrice: priceTmp
    })
  },

  orderConfirm(){
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 对当前页面js中的data数据，初始化操作可以在onLoad中完成
    var seatArrayTmp = []
    for(var i=0;i<135;i++){
      var seatObjectTmp = new Object()
      // console.log(Math.floor(i/15)+1,i%15+1)
      seatObjectTmp.id = i
      seatObjectTmp.row = Math.floor(i/15)+1
      seatObjectTmp.column = i%15+1
      seatObjectTmp.color = "white"
      seatArrayTmp.push(seatObjectTmp)
    }   
    this.setData({
      movieName:wx.getStorageSync('movieName'),
      seatObjectArray: seatArrayTmp
    })
  },
  onMouseWheel: function(e) {
    const deltaY = e.detail.deltaY;
    let newScale = this.data.scale;
    if (deltaY < 0) {
      // 向上滚动，放大
      newScale = Math.min(newScale + 0.1, 4);
    } else {
      // 向下滚动，缩小
      newScale = Math.max(newScale - 0.1, 0.5);
    }
    this.setData({
      
      scale: newScale
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})